package com.mgm.advertiser.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mgm.advertiser.service.AdvertiserService;
import com.mgm.constant.GobalConstant;
import com.mgm.dao.AdvertiserMapper;
import com.mgm.page.DataGrid;
import com.mgm.pojo.Advertiser;
import com.mgm.pojo.AdvertiserExample;
import com.mgm.pojo.AdvertiserExample.Criteria;
import com.mgm.result.IMessage;
import com.mgm.utils.RandomCodeUtils;

/**
 * 
 * @ClassName: AdvertiserServiceImpl 
 * @Description: TODO(...) 
 * @author Andy Zhang
 * @date 2018年6月28日 下午8:54:33 
 *
 */
@Service
public class AdvertiserServiceImpl implements AdvertiserService {
	
	/** 默认广告主溢价等于null */
	protected final Integer PREMIUM = null; 

	@Autowired
	private AdvertiserMapper advertiserMapper;
	
	private Lock lock = new ReentrantLock();
	
	@Override
	public IMessage addAdvertiser(Advertiser advertiser) {
		
		if (advertiser.getEnabled() == null) advertiser.setEnabled(false);
		if (advertiser.getPremium() == null) advertiser.setPremium(PREMIUM);
		
		// 获取所有广告主Id
		List<String> advIdArr = advertiserMapper.selectAdvIds();
		//加锁......
		lock.lock();
		try {
			// 数据库中广告主ID与随机生成4位数（阿拉伯数字）进行比较
			boolean isExist = true;
			String code = null;
			while (isExist) {
				code = GobalConstant.AD_PRE + RandomCodeUtils.randomNumeric(4);
				isExist = advIdArr.contains(code);
			}
			advertiser.setAdvId(code);
			advertiserMapper.insert(advertiser);
		} finally {
			lock.unlock(); // 释放锁......
		}
		
		return IMessage.success();
	}

	@Override
	public IMessage deleteAdvertiser(Advertiser advertiser) {
		advertiserMapper.deleteByPrimaryKey(advertiser.getId());
		return IMessage.success();
	}

	@Override
	public IMessage deleteById(List<Integer> ids) {
		for (Integer id : ids) {
			advertiserMapper.deleteByPrimaryKey(id);
		}
		return IMessage.success();
	}

	@Override
	public IMessage updateAdvertiser(Advertiser advertiser) {
		advertiserMapper.updateByPrimaryKeySelective(advertiser);
		return IMessage.success();
	}

	@Override
	public IMessage isEnabled(Integer id, Boolean enabled) {
		Advertiser record = new Advertiser();
		record.setId(id);
		record.setEnabled(enabled);
		advertiserMapper.updateByPrimaryKeySelective(record);
		return IMessage.success();
	}
	
	@Override
	public IMessage modifyPremium(Integer id, Integer premium) {
		Advertiser record = new Advertiser();
		record.setId(id);
		record.setPremium(premium);
		advertiserMapper.updateByPrimaryKeySelective(record);
		return IMessage.success();
	}

	@Override
	public Advertiser selectById(Integer id) {
		return advertiserMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Advertiser> findAllNetByEnabled() {
		AdvertiserExample example = new AdvertiserExample();
		Criteria criteria = example.createCriteria();
		criteria.andEnabledEqualTo((byte) 1);
		criteria.andAdvTypeEqualTo(GobalConstant.ADER_TYPE_NET); //广告主
		criteria.andUrlIsNotNull();
		return advertiserMapper.selectByExample(example);
	}

	@Override
	public DataGrid findAll(Map<String, String> param, Integer pageNum, Integer pageSize) {
		//查询
		AdvertiserExample example = new AdvertiserExample();
		//分页处理
		PageHelper.startPage(pageNum, pageSize);
		//创建条件
		Criteria criteria = example.createCriteria();
		String account = param.get("account");
		String advId = param.get("adv_id");
		String name = param.get("name");
		String enabled = param.get("enabled");
		
		if(!StringUtils.isEmpty(account)) {
			criteria.andAccountLike("%" + account + "%");
		}
		if(!StringUtils.isEmpty(advId)) {
			criteria.andAdvIdLike("%" + advId + "%");
		}
		if(!StringUtils.isEmpty(name)) {
			criteria.andNameLike("%" + name + "%");
		}
		if(!StringUtils.isEmpty(enabled)) {
			criteria.andEnabledEqualTo(enabled.equals("true") ? (byte)1 : (byte) 0);
		}
		
		List<Advertiser> list = advertiserMapper.selectByExample(example);
		//创建一个返回值对象
		DataGrid dataGrid = new DataGrid();
		dataGrid.setItems(list);
		//获取PageInfo相关信息
		PageInfo<Advertiser> pageInfo = new PageInfo<>(list);
		dataGrid.setTotal_count(pageInfo.getTotal()); //总记录数
		dataGrid.setPage_count(pageInfo.getPages()); // 总页数
		dataGrid.setPage_index(pageInfo.getPageNum()); // 当前页
		
		return dataGrid;
	}

	@Override
	public List<Advertiser> findByName(String name) {
		AdvertiserExample example = new AdvertiserExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name);
		return advertiserMapper.selectByExample(example);
	}
	
	@Override
	public List<Advertiser> findByNameLike(String name) {
		AdvertiserExample example = new AdvertiserExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameLike("%" +name+ "%");
		return advertiserMapper.selectByExample(example);
	}

}
